﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AD.Model;
using AD.BLL;
public partial class Manager_User_PageData_ReportConsumption : WebManagerForm
{
    public List<AccountFlowMod> InviteFlows = new List<AccountFlowMod>();
    public int currPageIndex = 1;
    public int MaxSize = 0;
    public int MaxPage = 0;
    public string userName = "";
    public string startTime = "";
    public string endTime = "";
    public string operationName;
    protected void Page_Load(object sender, EventArgs e)
    {
        string indexstr = Request.QueryString["pageIndex"];
        string action = Request.QueryString["action"];
        string idstrs = Request.Params["ids"];
        string idstr = Request.Params["id"];
        userName = Request.Params["userName"];
        startTime = Request.Params["startTime"];
        endTime = Request.Params["endTime"];
        operationName = Request.Params["operationName"];
        string filter = " RewardType=1 ";
        if (!string.IsNullOrEmpty(startTime))
            filter = string.Format(" and Commit_Date >= '%{0}%' ", startTime);
        if (!string.IsNullOrEmpty(endTime))
            filter += string.Format(" and Commit_Date<='{0}' ", endTime);
        if (!string.IsNullOrEmpty(operationName))
            filter += string.Format(" and operationName='{0}' ", operationName);
        if (!string.IsNullOrEmpty(action))
        {
            if (action.Equals("gopage"))
            {
                int currPageIndex;
                bool effect = int.TryParse(indexstr, out currPageIndex);
                SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
            else if (action.Equals("queryFilter")) {
                SetDataBind(userName, filter, UtilHepler.PAGE_SIZE, currPageIndex);
            }
        }
    }

    public string GetUserName(int id)
    {
        UserMod user = UserBLL.QueryById(id);
        if (user != null) return user.Email;
        return string.Empty;
    }

    void SetDataBind(string name, string filter, int pageSize, int index)
    {
        if (!string.IsNullOrEmpty(userName))
        {
            List<UserMod> users = UserBLL.QueryFilterToList(" Email=%" + userName + "'");
            if (users != null)
            {
                foreach (UserMod user in users)
                {
                    string tmpFilter = filter + " and UserId=" + user.AutoID;
                    List<AccountFlowMod> tmp = AccountFlowBLL.QueryByPage(tmpFilter, pageSize, index == 0 ? 0 : index - 1, out MaxSize);
                    if (tmp != null && tmp.Count > 0)
                    {
                        InviteFlows.AddRange(tmp);
                        if (InviteFlows.Count > pageSize)
                        {
                            InviteFlows.RemoveRange(10, InviteFlows.Count - pageSize);
                            break;
                        }
                    }
                }
            }
        }
        else
        {
            InviteFlows = AccountFlowBLL.QueryByPage(filter, pageSize, index == 0 ? 0 : index - 1, out MaxSize);
            MaxPage = MaxSize / UtilHepler.PAGE_SIZE;
            if (MaxSize % UtilHepler.PAGE_SIZE != 0)
                MaxPage += 1;
        }
        if (InviteFlows == null)
            InviteFlows = new List<AccountFlowMod>();

        foreach (AccountFlowMod account in InviteFlows)
        {
            UserMod tmpUser = UserBLL.QueryById(account.AutoID);
            if (tmpUser != null)
                account.UserName = tmpUser.Email;
        }
    }
}